/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewVentePan.java
 *
 * Created on 20 août 2011, 16:38:04
 */
package app.views;

import app.App;

import app.controllers.Cahier;
import app.models.Lignevente;
import app.models.Vente;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author Ghost
 */
public class NewVentePan extends javax.swing.JPanel implements ActionListener {

    Cahier cahier;
    
    private Vente vente;
    

    /** Creates new form NewVentePan */
    public NewVentePan() {
        vente = Cahier.prepareNewVente();
        initComponents();
        venteGui.setVente(vente);
        venteGui.addActionListener(this);
    }

    public void setCahier(Cahier cahier) {
        this.cahier = cahier;
    }
    
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        venteGui = new app.views.VenteGui();
        actionPan = new javax.swing.JPanel();
        btValider = new javax.swing.JButton();
        btVider = new javax.swing.JButton();
        btAjouter = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(venteGui, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(venteGui, javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE)
                .addContainerGap())
        );

        btValider.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btValider.setIcon(App.createIcon("resources/accept.png"));
        btValider.setText("Valider la vente");
        btValider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btValiderActionPerformed(evt);
            }
        });

        btVider.setText("Vider");
        btVider.setBorderPainted(false);
        btVider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btViderActionPerformed(evt);
            }
        });

        btAjouter.setText("Ajouter");
        btAjouter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAjouterActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout actionPanLayout = new javax.swing.GroupLayout(actionPan);
        actionPan.setLayout(actionPanLayout);
        actionPanLayout.setHorizontalGroup(
            actionPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, actionPanLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btAjouter)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btVider, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(btValider)
                .addContainerGap())
        );
        actionPanLayout.setVerticalGroup(
            actionPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(actionPanLayout.createSequentialGroup()
                .addGroup(actionPanLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btVider, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btValider)
                    .addComponent(btAjouter))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(actionPan, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(actionPan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void btValiderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btValiderActionPerformed
    if(vente.getLigneventeList().size() <= 0){
        App.displayMessage("Il n'y a pas de produit dans la liste!", App.MESSAGE_TYPE.ERROR);
        return;
    }
    if("".equals(vente.getType())){
        App.displayMessage("Le mode paiement n'a pas été sélectionné", App.MESSAGE_TYPE.ERROR);
        return;
    }
    App.getInstance().getCahier().saveVente(vente);
    App.displayMessage("La vente a été enregistrée\nAu suivant!", App.MESSAGE_TYPE.INFO);
    vente = Cahier.prepareNewVente();
    //cleanup
    venteGui.setVente(null);
    venteGui.setVente(vente);
}//GEN-LAST:event_btValiderActionPerformed

private void btViderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btViderActionPerformed
    vente.cancel();
    vente = Cahier.prepareNewVente();
    venteGui.setVente(vente);
    venteGui.sync();
}//GEN-LAST:event_btViderActionPerformed

private void btAjouterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAjouterActionPerformed
    ReferencesDialog ref = new ReferencesDialog();
    Lignevente lv = ref.showDialog();
    if(lv != null){
        vente.ajouterLigneVente(lv);
        venteGui.sync();
    }
}//GEN-LAST:event_btAjouterActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel actionPan;
    private javax.swing.JButton btAjouter;
    private javax.swing.JButton btValider;
    private javax.swing.JButton btVider;
    private javax.swing.JPanel jPanel1;
    private app.views.VenteGui venteGui;
    // End of variables declaration//GEN-END:variables

    @Override
    public void actionPerformed(ActionEvent e) {
        if(VenteGui.CLIENT_BTN.equals(e.getSource())){
            ClientsDialog dial = new ClientsDialog();
            dial.showDialog();
            vente.setClient(dial.getSelectedClient());
            venteGui.sync();
        } else if(VenteGui.RB_CB.equals(e.getSource())){
            vente.setType(Vente.TYPE_CB);
            venteGui.sync();
        } else if(VenteGui.RB_CHEQUE.equals(e.getSource())){
            vente.setType(Vente.TYPE_CHEQUE);
            venteGui.sync();
        } else if(VenteGui.RB_ESPECES.equals(e.getSource())){
            vente.setType(Vente.TYPE_ESPECES);
            venteGui.sync();
        }
    }

}
